package com.snowfield.lb47.lbconfig.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.snowfield.lb47.lbconfig.domain.BlackwhiteList;
import com.snowfield.lb47.lbconfig.domain.HttpCheckParam;
import com.snowfield.lb47.lbconfig.mapper.HttpCheckParamMapper;
import com.snowfield.lb47.lbconfig.service.IHttpCheckParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * HTTP检测参数Service业务层处理
 *
 * @author snowfield
 * @date 2024-05-28
 */
@Service
public class HttpCheckParamServiceImpl implements IHttpCheckParamService {
    @Autowired
    private HttpCheckParamMapper httpCheckParamMapper;

    /**
     * 查询HTTP检测参数
     *
     * @param id HTTP检测参数主键
     * @return HTTP检测参数
     */
    @Override
    public HttpCheckParam selectHttpCheckParamById(Long id) {
        return httpCheckParamMapper.selectHttpCheckParamById(id);
    }

    /**
     * 查询HTTP检测参数列表
     *
     * @param httpCheckParam HTTP检测参数
     * @return HTTP检测参数
     */
    @Override
    public List<HttpCheckParam> selectHttpCheckParamList(HttpCheckParam httpCheckParam) {
        return httpCheckParamMapper.selectHttpCheckParamList(httpCheckParam);
    }

    /**
     * 新增HTTP检测参数
     *
     * @param httpCheckParam HTTP检测参数
     * @return 结果
     */
    @Override
    public int insertHttpCheckParam(HttpCheckParam httpCheckParam) {
        httpCheckParam.setCreateTime(DateUtils.getNowDate());
        //非https时，sni不配置
        if (httpCheckParam.getHttps().equals("N")){
            httpCheckParam.setSni("");
        }
        if (httpCheckParam.getExpectType().equals("responseCode")){
            //配置响应码时，响应内容不配置
            httpCheckParam.setExpectContent("");
        } else if (httpCheckParam.getExpectType().equals("responseContent")){
            //配置响应内容时，响应码不配置
            httpCheckParam.setExpectStatus("");
        }

        return httpCheckParamMapper.insertHttpCheckParam(httpCheckParam);
    }

    /**
     * 修改HTTP检测参数
     *
     * @param httpCheckParam HTTP检测参数
     * @return 结果
     */
    @Override
    public int updateHttpCheckParam(HttpCheckParam httpCheckParam) {
        httpCheckParam.setUpdateTime(DateUtils.getNowDate());

        if (httpCheckParam.getHttps().equals("N")){
            httpCheckParam.setSni("");
        }
        if (httpCheckParam.getExpectType().equals("responseCode")){
            httpCheckParam.setExpectContent("");
        } else if (httpCheckParam.getExpectType().equals("responseContent")){
            httpCheckParam.setExpectStatus("");
        }
        return httpCheckParamMapper.updateHttpCheckParam(httpCheckParam);
    }

    /**
     * 批量删除HTTP检测参数
     *
     * @param ids 需要删除的HTTP检测参数主键
     * @return 结果
     */
    @Override
    public int deleteHttpCheckParamByIds(Long[] ids) {
        //判断检测参数是否已经分配，若已经分配不可以删除
        for (Long paramId : ids) {
            HttpCheckParam httpCheckParam = httpCheckParamMapper.selectHttpCheckParamById(paramId);

            if (httpCheckParam == null) {
                continue;
            }
            int count = httpCheckParamMapper.usedHttpCheckParamCount(paramId);
            if (count > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", httpCheckParam.getParamName()));
            }
        }
        return httpCheckParamMapper.deleteHttpCheckParamByIds(ids);
    }

    /**
     * 删除HTTP检测参数信息
     *
     * @param id HTTP检测参数主键
     * @return 结果
     */
    @Override
    public int deleteHttpCheckParamById(Long id) {
        return httpCheckParamMapper.deleteHttpCheckParamById(id);
    }

    /**
     * 校验HTTP检测参数名称是否唯一
     *
     * @param httpCheckParam HTTP检测参数
     * @return 结果
     */
    @Override
    public Boolean checkNameUnique(HttpCheckParam httpCheckParam){
        Long paramId = httpCheckParam.getId();
        HttpCheckParam checkParam = httpCheckParamMapper.selectHttpCheckParamByName(httpCheckParam.getParamName());
        if (checkParam != null && !checkParam.getId().equals(paramId)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
